
package com.hospital.diagnose.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.diagnose.entity.Patient;
import com.hospital.diagnose.service.DiagnoseService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;


/**
* @Description:
* @Param:
* @return:
* @Date: 2019/1/3 13:18
*/


@Controller
@RequestMapping(value = "/convenient")
public class DiagnoseController {
    @Autowired
    DiagnoseService diagnoseService;

    @RequestMapping(value = "/all",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        return diagnoseService.selectAllPat();
    }

    @RequestMapping ( value = "/add",produces = "application/json;charset=utf-8", method = RequestMethod.POST )
    @ResponseBody
    public String insertPat(@RequestBody String data){
        int sex;
        JSONObject jsonObject=JSON.parseObject(data);
        String patName=jsonObject.getString("patName");
        String patSex=jsonObject.getString("patSex");
        if (patSex.equals("女")){
            sex=0;
        }else {
            sex=1;
        }
        Integer patAge=jsonObject.getInteger("patAge");
        String patContactInfo=jsonObject.getString("patContactInfo");
        String patNation=jsonObject.getString("patNation");
        String patDiseaseHistory=jsonObject.getString("patDiseaseHistory");

        return diagnoseService.insertPat(patName,sex,patAge,patContactInfo,patNation,patDiseaseHistory);

    }


    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String updatePat(@RequestBody String data){
        int sex;
        String patId=JSON.parseObject(data).getString("id");
        String patName=JSON.parseObject(data).getString("patName");
        String patSex=JSON.parseObject(data).getString("patSex");
        if (patSex.equals("女")){
            sex=0;
        }else {
            sex=1;
        }
        Integer patAge=JSON.parseObject(data).getInteger("patAge");
        String patContactInfo=JSON.parseObject(data).getString("patContactInfo");
        String patNation=JSON.parseObject(data).getString("patNation");
        String patDiseaseHistory=JSON.parseObject(data).getString("patDiseaseHistory");
        Patient patient=diagnoseService.selectByPrimaryKey(Integer.parseInt(patId));
        patient.setPatName(patName);
        patient.setPatSex(sex);
        patient.setPatAge(patAge);
        patient.setPatContactInfo(patContactInfo);
        patient.setPatDiseaseHistory(patDiseaseHistory);
        patient.setPatNation(patNation);

        return diagnoseService.updatePatByPatient(patient);

    }


    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String deleteById(@RequestBody String  data){
        System.out.println(data);
        String string = JSON.parseObject(data).getString("id");
        return diagnoseService.deleteById(Integer.parseInt(string));
    }





}
